package mock.servicesdirectory.contacts.web.action;

import java.util.List;

import mock.servicesdirectory.contacts.business.businessinterface.ContactBo;
import mock.servicesdirectory.contacts.web.model.ContactModel;
import com.opensymphony.xwork2.ActionSupport;


public class ListContactAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;			
	
	/*
	 * Property that won't be serialable to json type
	 */
	private String firstName;
	private String surName;
	private boolean isActive;
	
	/*
	 * Property that containt Business Object that use Dao to retrive database 
	 * and process all business rules
	 */
	private ContactBo contactBo;
	
	/*
	 *All property that use for grid jquery's tag 
	 */
	private List<ContactModel> gridModel;	
	private Integer rows = 15;	
	private Integer page = 0;	
	private String sord;		
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;	
	private String filters;
	private Integer total = 0;
	private Integer records = 0;
	private boolean loadonce = false;
	
	/*
	 * List ContactModel that will be presented into grid view
	 */
	List<ContactModel> contacts;
	
	
	public String execute() {										
		listContacts();								
		setGridModel(contacts);		
		setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));		
		
		return SUCCESS;
	}

	public String getJSON() {
		return execute();
	}
	
	/**
	 * Method used for searchContactAction
	 * @return
	 */
	public String search(){
		return execute();
	}	
	
	/**
	 * Method used to get list of contats by call Business Object to retrive database
	 * and process business rules
	 */
	public void listContacts(){
		isActive=false;		
		
		contactBo.initContacts();		
		contacts = contactBo.getContactWithFilter(firstName, surName, isActive);								
		
		setRecords(contacts.size());
		int to = (getRows() * getPage());
        int from = to - getRows();
        if (to > getRecords()) {
            to = getRecords();
        }        
        contacts = contactBo.findContact(contacts, from, to);
        
        
        /*
         * Test Script with search for grid data
         */
        /*if (searchString != null && searchOper != null && !searchString.equals("") 
        		&& !searchOper.equals("")) {                       
            if (searchOper.equalsIgnoreCase("eq")) {
                System.out.println("Searching Equality");                
                contacts = contactBo.getContactWithFilterFirstName(contacts, searchString);             
            }
        }*/ 
	}
	
	/**
	 * Method used to process contactCreateAction action
	 */
	public String create(){
		return SUCCESS;
	}
	
	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param surName the surName to set
	 */
	public void setSurName(String surName) {
		this.surName = surName;
	}

	/**
	 * @return the surName
	 */
	public String getSurName() {
		return surName;
	}

	/**
	 * @param isActive the isActive to set
	 */
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * @return the isActive
	 */
	public boolean isActive() {
		return isActive;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}	

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public List<ContactModel> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<ContactModel> gridModel) {
		this.gridModel = gridModel;
	}

	/**
	 * @return the sidx
	 */
	public String getSidx() {
		return sidx;
	}

	/**
	 * @param sidx the sidx to set
	 */
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	/**
	 * @return the searchField
	 */
	public String getSearchField() {
		return searchField;
	}

	/**
	 * @param searchField the searchField to set
	 */
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	/**
	 * @return the searchOper
	 */
	public String getSearchOper() {
		return searchOper;
	}

	/**
	 * @param searchOper the searchOper to set
	 */
	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	/**
	 * @return the searchString
	 */
	public String getSearchString() {
		return searchString;
	}

	/**
	 * @param searchString the searchString to set
	 */
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	/**
	 * @return the contactBo
	 */
	public ContactBo getContactBo() {
		return contactBo;
	}

	/**
	 * @param contactBo the contactBo to set
	 */
	public void setContactBo(ContactBo contactBo) {
		this.contactBo = contactBo;
	}

	/**
	 * @param loadonce the loadonce to set
	 */
	public void setLoadonce(boolean loadonce) {
		this.loadonce = loadonce;
	}

	/**
	 * @return the loadonce
	 */
	public boolean isLoadonce() {
		return loadonce;
	}

	/**
	 * @param records the records to set
	 */
	public void setRecords(Integer records) {
		this.records = records;
        if (this.records > 0 && this.getRows() > 0) {
            this.total = (int) Math.ceil((double) this.records / (double) this.getRows());
        } else {
            this.total = 0;
        }
	}

	/**
	 * @return the records
	 */
	public Integer getRecords() {
		return records;
	}

	/**
	 * @param rows the rows to set
	 */
	public void setRows(Integer rows) {
		this.rows = rows;
	}

	/**
	 * @return the rows
	 */
	public Integer getRows() {
		return rows;
	}

	/**
	 * @param filters the filters to set
	 */
	public void setFilters(String filters) {
		this.filters = filters;
	}

	/**
	 * @return the filters
	 */
	public String getFilters() {
		return filters;
	}	
}